package com.mission.schedule.CommonDialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.BackgroundColorSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.mission.schedule.R;
import com.mission.schedule.applcation.App;
import com.mission.schedule.clock.QueryAlarmData;
import com.mission.schedule.constants.Const;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.my160920.widget.EditTextC;
import com.mission.schedule.utils.SharedPrefUtil;

/**
 * 日程内容编辑对话框
 * Created by liwei on 2017/4/18.
 */

public class CalenderColorDialog extends Dialog {


    CallBack callBack;

    SharedPrefUtil sharedPrefUtil;
    Context context;
    WindowManager windowManager;
    EditTextC content;
    View view;
    TextView color1_tv, color2_tv, color3_tv, color4_tv, color5_tv, color6_tv;
    int colors[] = {R.color.white, R.color.week_color, R.color.color4, R.color.mingtian_color, R.color.newlogin_origin, R.color.blue};
    String schID_str;
    int type = 0;

    public CalenderColorDialog(@NonNull final Context context, @StyleRes int themeResId, WindowManager windowManager, final String schID, final int type) {
        super(context, themeResId);
        this.context = context;
        this.windowManager = windowManager;

        setCanceledOnTouchOutside(true);

        view = LayoutInflater.from(context).inflate(R.layout.calender_check_color, null);
        setContentView(view);

        schID_str = schID;
        this.type = type;
        sharedPrefUtil = new SharedPrefUtil(context, ShareFile.USERFILE);

        color1_tv = view.findViewById(R.id.color1_tv);
        color2_tv = view.findViewById(R.id.color2_tv);
        color3_tv = view.findViewById(R.id.color3_tv);
        color4_tv = view.findViewById(R.id.color4_tv);
        color5_tv = view.findViewById(R.id.color5_tv);
        color6_tv = view.findViewById(R.id.color6_tv);

        color1_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setContentColor(0);
            }
        });
        color2_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setContentColor(1);
            }
        });
        color3_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setContentColor(2);
            }
        });
        color4_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setContentColor(3);
            }
        });
        color5_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setContentColor(4);
            }
        });
        color6_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setContentColor(5);
            }
        });


    }

    int color_position = 0;

    private void setContentColor(int position) {
        color_position = position;
        if (position > 2) {
            color_position_f = "1";
        } else {
            color_position_f = "0";
        }
        save();
    }

    String color_position_f = "0";//1白色

    void save() {
        if (!schID_str.isEmpty()) {
            App.getDBcApplication().updataTaskScheduleDataColor(schID_str, color_position_f, color_position + "");
            if (type == 0) {
                QueryAlarmData.writeAlarm(context.getApplicationContext());
                Intent intent = new Intent(Const.SHUAXINDATA);
                intent.putExtra("data", "success");
                intent.putExtra("index", 1);
                intent.putExtra("what", 2);
                context.sendBroadcast(intent);
            }
        }
        callBack.reminderEditTime(color_position_f, color_position + "");
        dismiss();
    }


//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        /* 触摸外部弹窗 */
//        if (isOutOfBounds(getContext(), event)) {
//            save();
//            return false;
//        }
//        return super.onTouchEvent(event);
//    }

//    private boolean isOutOfBounds(Context context, MotionEvent event) {
//        final int x = (int) event.getX();
//        final int y = (int) event.getY();
//        final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
//        final View decorView = getWindow().getDecorView();
//        return (x < -slop) || (y < -slop) || (x > (decorView.getWidth() + slop)) || (y > (decorView.getHeight() + slop));
//    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        window.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        params.width = windowManager.getDefaultDisplay().getWidth();
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
    }

    public interface CallBack {
        void reminderEditTime(String color_position_f, String color_position);
    }

    public CallBack getCallBack() {
        return callBack;
    }

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }

}
